package com.crm.business.crm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.crm.business.crm.mapper.HpPaymentTypeDictMapper;
import com.crm.business.crm.service.CrmPageService;
import com.crm.business.crm.service.IHpPaymentTypeDictService;
import com.crm.business.crm.service.BillPaymentDeptBindService;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.crm.entity.PO.HpPaymentTypeDict;
import com.crm.common.crm.entity.PO.BillPaymentDeptBind;
import com.crm.common.crm.entity.VO.CrmModelFiledVO;
import com.crm.business.servlet.BaseServiceImpl;
import com.crm.business.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 线索表 服务实现类
 * </p>
 *
 * @author zhangzhiwei
 * @since 2020-05-21
 */
@Service
@Slf4j
public class HpPaymentTypeDictServiceImpl extends BaseServiceImpl<HpPaymentTypeDictMapper, HpPaymentTypeDict> implements IHpPaymentTypeDictService, CrmPageService {


    @Autowired
    private BillPaymentDeptBindService wkPaymentDeptBindService;

    @Override
    public List<HpPaymentTypeDict> queryPaymentTypeDicListByParentId(Integer parentId) {
        List<HpPaymentTypeDict> result = new ArrayList<>();
        List<HpPaymentTypeDict> paymentList = this.baseMapper.queryAll();


        if(CollectionUtil.isNotEmpty(paymentList)){
            Integer deptId = UserUtil.getUser().getDeptId();
            List<BillPaymentDeptBind> bindList = wkPaymentDeptBindService.queryByDeptId(deptId);

            if(CollectionUtil.isNotEmpty(bindList)){
                List<Integer> threeIds = bindList.stream().map(BillPaymentDeptBind::getPaymentId).collect(Collectors.toList());
                List<HpPaymentTypeDict> threeList = paymentList.stream().filter(dict->threeIds.contains(dict.getId())).collect(Collectors.toList());

                List<Integer> twoIds = threeList.stream().map(HpPaymentTypeDict::getParentId).collect(Collectors.toList());
                List<HpPaymentTypeDict> twoList = paymentList.stream().filter(dict->twoIds.contains(dict.getId())).collect(Collectors.toList());

                List<Integer> oneIds = twoList.stream().map(HpPaymentTypeDict::getParentId).collect(Collectors.toList());
                List<HpPaymentTypeDict> oneList = paymentList.stream().filter(dict->oneIds.contains(dict.getId())).collect(Collectors.toList());

                List<HpPaymentTypeDict> checkList = new ArrayList<>();
                checkList.addAll(oneList);
                checkList.addAll(twoList);
                checkList.addAll(threeList);
                paymentList = checkList;
            }
        }


        if(CollectionUtil.isNotEmpty(paymentList)){
            List<HpPaymentTypeDict> oneList = paymentList.stream().filter(dict-> String.valueOf(1).equals(dict.getPaymentLevel())).collect(Collectors.toList());
            List<HpPaymentTypeDict> twoList = paymentList.stream().filter(dict-> String.valueOf(2).equals(dict.getPaymentLevel())).collect(Collectors.toList());
            List<HpPaymentTypeDict> threeList = paymentList.stream().filter(dict-> String.valueOf(3).equals(dict.getPaymentLevel())).collect(Collectors.toList());

            Map<Integer, List<HpPaymentTypeDict>> threeMap = threeList.stream().collect(Collectors.groupingBy(HpPaymentTypeDict::getParentId));
            if(Optional.ofNullable(threeMap).isPresent()){
                for(HpPaymentTypeDict twoDict : twoList){
                    twoDict.setChildList(threeMap.get(twoDict.getId()));
                }
            }

            Map<Integer, List<HpPaymentTypeDict>> twoMap = twoList.stream().collect(Collectors.groupingBy(HpPaymentTypeDict::getParentId));
            if(Optional.ofNullable(twoMap).isPresent()){
                for(HpPaymentTypeDict oneDict : oneList){
                    oneDict.setChildList(twoMap.get(oneDict.getId()));
                }
            }
            result.addAll(oneList);
        }

//        List<HpPaymentTypeDict> paymentList = this.baseMapper.queryPaymentTypeDicListByParentId(0);
//        for(HpPaymentTypeDict paymentTypeDict : paymentList){
//            List<HpPaymentTypeDict> childList = this.baseMapper.queryPaymentTypeDicListByParentId(paymentTypeDict.getId());
//            paymentTypeDict.setChildList(childList);
//        }
        return result;
    }

    @Override
    public String[] appendSearch() {
        return new String[0];
    }

    @Override
    public void setOtherField(Map<String, Object> map) {

    }

    @Override
    public CrmEnum getLabel() {
        return null;
    }

    @Override
    public List<CrmModelFiledVO> queryDefaultField() {
        return null;
    }
}
